package com.zixi.ai.workflow.service.impl;

import com.agentsflex.core.util.StringUtil;
import com.zixi.ai.file.service.IFileService;
import com.zixi.ai.workflow.service.ITinyFlowConfigService;
import dev.tinyflow.core.Tinyflow;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author wfg
 */
@RequiredArgsConstructor
@Service
public class TinyFlowConfigServiceImpl implements ITinyFlowConfigService {
    private final IFileService fileService;
    @Override
    public void setExtraNodeParser(Tinyflow tinyflow) {

    }

    @Override
    public void setSearchEngineProvider(Tinyflow tinyflow) {

    }

    @Override
    public void setLlmProvider(Tinyflow tinyflow) {
        tinyflow.setLlmProvider(id -> null);
    }

    @Override
    public void setKnowledgeProvider(Tinyflow tinyflow) {

    }

    @Override
    public void setFileStorage(Tinyflow tinyflow) {
        tinyflow.setFileStorage((stream, headers) -> {
            String name = UUID.randomUUID().toString();
            if (headers != null) {
                String contentDisposition = headers.get("Content-Disposition");
                if (StringUtil.hasText(contentDisposition)) {
                    name = contentDisposition.split(";")[1].split("=")[1].replace("\"", "");
                }

                String contentType = headers.get("Content-Type");
                if (StringUtil.hasText(contentType)) {
                    String suffix = contentType.split("/")[1];
                    name = UUID.randomUUID() + "." + suffix;
                }
            }
            return fileService.upload(name, stream);
        });
    }
}
